package com.allwees.bs.c.common.filter;

import com.allwees.bs.c.module.user.service.DeviceService;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(urlPatterns = "/oauth/token",filterName = "loginFilter")
public class LoginFilter implements Filter {

    @Resource
    private DeviceService deviceService;

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        filterChain.doFilter(servletRequest,servletResponse);
        String username = servletRequest.getParameter("username");
        String pushId = servletRequest.getParameter("pushId");
        if(StringUtils.isAnyBlank(username,pushId)){
            return;
        }
        deviceService.useDeviceLogin(username,pushId);
    }
}